﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;


namespace BuienradarAppWeb.Pages
{
    public partial class BuienradarAppPart : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            string buienradarUrl = "";

            /*Register at http://www.microsoft.com/maps/ to get a key*/
            string BingMapKey ="AvL7zxxtdfoqL_o99O7qoes1H150Cq4Mlx0866aeoP92xP6b6_lfyJLI3PM7vh0P";
            string BingMapUrl = "http://dev.virtualearth.net/REST/v1/Locations?o=xml&countryRegion={0}&locality={1}&postalCode={2}&userIp=127.0.0.1&maxResults=1&key={3}";
            string url;
            string width = "256";
            string height = "256";
            string country = "nl";
            string city= "Eindhoven";
            string postalcode= "5653PX";


            decimal widthFrame = 226;
            decimal heightFrame = 226;
            decimal latitude;
            decimal longitude;
            XElement root=null; 
            try
            {
                width = Request.QueryString["width"];
                height = Request.QueryString["height"];
                country = Request.QueryString["country"];
                city = Request.QueryString["city"];
                postalcode = Request.QueryString["postalcode"];
                widthFrame = decimal.Parse(width) - 30;
                heightFrame = decimal.Parse(height) -30 ;
                
                url= String.Format(BingMapUrl,country,city,postalcode,BingMapKey);

                try
                {
                    root = XElement.Load(url);
                }
                catch (Exception)
                {

                }                 

                 if (root != null &&
                     decimal.TryParse(root.XPathSelectElement("//Location/Point/Latitude").Value, out latitude) &&
                    decimal.TryParse(root.XPathSelectElement("//Location/Point/Longitude").Value, out longitude))
                 {
            
                    buienradarUrl = String.Format("http://mijn.buienradar.nl/lokalebuienradar.aspx?voor=1&lat={0}&x=1&y=1&lng={1}&overname=2&zoom=8&naam=&size=2&map=1",
                                            latitude, longitude);
                 }
                 else
                 {
                     buienradarUrl =String.Format("http://www2.buienradar.nl/display.php?width={0}&height={1}&country={2}&maptype=2&opacity=75",
                                            width,height,country);
                 }

                 litBuienradar.Text = String.Format(@"
                        <IFRAME 
                            SRC='{0}' 
                            NORESIZE 
                            SCROLLING=NO 
                            HSPACE=0 
                            VSPACE=0 FRAMEBORDER=0 MARGINHEIGHT=0 MARGINWIDTH=0 
                            WIDTH={1} 
                            HEIGHT={2}>
                        </IFRAME>", buienradarUrl, widthFrame.ToString(), heightFrame.ToString());

            }
            catch (Exception ex)
            {
                litBuienradar.Text = "Er ging iets mis: " + ex.Message;

            }


        }
    }
}